package com.xbongbong.saas.enums;

import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.enums.PaySheetTypeEnum;
import com.xbongbong.paas.enums.PaymentSheetTypeEnum;
import com.xbongbong.saas.constant.BasicConstant;
import lombok.Getter;

import java.util.List;
import java.util.Objects;

/**
 * 协同团队
 * @author haibin.zhang
 * @version v1.0
 * @date 2019/2/19 19:01
 * @since v1.0
 */
@Getter
public enum UserTeamEnum {
    // 协同团队
    CUSTOMER_TEAM(XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode(),"enum.userTeam.attr.customer_team","", IndexTypeEnum.IDX_SAAS_CUSTOMER_TEAM),
    OPPORTUNITY_TEAM(XbbRefTypeEnum.SALES_OPPORTUNITY.getCode(),"enum.userTeam.attr.opportunity_team","", IndexTypeEnum.IDX_SAAS_OPPORTUNITY_TEAM),
    CONTRACT_TEAM(XbbRefTypeEnum.CONTRACT.getCode(),"enum.userTeam.attr.contract_team","", IndexTypeEnum.IDX_SAAS_CONTRACT_TEAM),
    CONTACT_TEAM(XbbRefTypeEnum.CONTACT.getCode(),"enum.userTeam.attr.contact_team","", IndexTypeEnum.IDX_SAAS_CONTACT_TEAM),
    SUPPLIER_TEAM(XbbRefTypeEnum.SUPPLIER.getCode(),"enum.userTeam.attr.supplier_team","", IndexTypeEnum.IDX_SAAS_SUPPLIER_TEAM),
    PURCHASE_TEAM(XbbRefTypeEnum.PURCHASE.getCode(),"enum.userTeam.attr.purchase_team","", IndexTypeEnum.IDX_SAAS_PURCHASE_TEAM),
    PRODUCTIONORDER_TEAM(XbbRefTypeEnum.PRODUCTION_ORDER.getCode(),"enum.userTeam.attr.productionorder_team","", IndexTypeEnum.IDX_SAAS_PRODUCTION_ORDER_TEAM),
    BOMBILL_TEAM(XbbRefTypeEnum.BOM_BILL.getCode(),"enum.userTeam.attr.bombill_team","", IndexTypeEnum.IDX_SAAS_BOM_TEAM),
    PRODUCT_TEAM(XbbRefTypeEnum.PRODUCT.getCode(),"enum.userTeam.attr.product_team","", IndexTypeEnum.IDX_SAAS_PRODUCT_TEAM),
    WORK_ORDER_TEAM(XbbRefTypeEnum.WORK_ORDER.getCode(),"enum.userTeam.attr.work_order_team","", IndexTypeEnum.IDX_SAAS_WORK_ORDER_TEAM),
    REFUND_TEAM(XbbRefTypeEnum.REFUND.getCode(),"enum.userTeam.attr.refund_team","", IndexTypeEnum.IDX_SAAS_REFUND_TEAM),
    SUPPLIER_CONTACT_TEAM(XbbRefTypeEnum.SUPPLIER_CONTACT.getCode(),"enum.userTeam.attr.supplier_contact_team","", IndexTypeEnum.IDX_SAAS_SUPPLIER_CONTACT_TEAM),
    PAYMENT_TEAM(XbbRefTypeEnum.PAYMENT.getCode(),"enum.userTeam.attr.payment_team","", IndexTypeEnum.IDX_SAAS_PAYMENT_TEAM),
    PAY_PLAN_TEAM(XbbRefTypeEnum.PAY_PLAN.getCode(),"enum.userTeam.attr.pay_plan_team","", IndexTypeEnum.IDX_SAAS_PAY_PLAN_TEAM),
    WAREHOUSE_TEAM(XbbRefTypeEnum.WAREHOUSE.getCode(),"enum.userTeam.attr.warehouse_team","", null),
    QUOTATION_TEAM(XbbRefTypeEnum.QUOTATION.getCode(),"enum.userTeam.attr.quotation_team","", IndexTypeEnum.IDX_SAAS_QUOTATION_TEAM),
    PAYMENT_BALANCE_TEAM(XbbRefTypeEnum.PREPAYMENT_BALANCE.getCode(),"enum.userTeam.attr.payment_balance_team","", IndexTypeEnum.IDX_PAYMENT_BALANCE_TEAM),
    PAY_BALANCE_TEAM(XbbRefTypeEnum.PREPAY_BALANCE.getCode(),"enum.userTeam.attr.pay_balance_team","", IndexTypeEnum.IDX_PAY_BALANCE_TEAM),
    PAY_SHEET_TEAM(XbbRefTypeEnum.PAY_SHEET.getCode(),"enum.userTeam.attr.pay_sheet_team","",IndexTypeEnum.IDX_SAAS_PAYPLAN_SHEET_TEAM),
    PAYMENT_SHEET_TEAM(XbbRefTypeEnum.PAYMENT_SHEET.getCode(),"enum.userTeam.attr.payment_sheet_team","",IndexTypeEnum.IDX_SAAS_PAYMENT_SHEET_TEAM),
    CLUE_TEAM(XbbRefTypeEnum.CLUE.getCode(),"enum.userTeam.attr.clue_team","",IndexTypeEnum.IDX_SAAS_CLUE_TEAM),
    MARKET_ACTIVITY_TEAM(XbbRefTypeEnum.MARKET_ACTIVITY.getCode(),"enum.userTeam.attr.market_activity_team","",IndexTypeEnum.IDX_SAAS_MARKET_ACTIVITY_TEAM),
    /**
     * 经销商，以下仅作为消息、日志等区分使用，存储数据直接用客户(CUSTOMER_TEAM)
     */
    DISTRIBUTOR_TEAM(XbbRefTypeEnum.DISTRIBUTOR_MANAGEMENT.getCode(),"enum.userTeam.attr.distributor_team","", IndexTypeEnum.IDX_SAAS_CUSTOMER_TEAM),
    RETURN_TEAM(XbbRefTypeEnum.RETURN.getCode(),"enum.userTeam.attr.refund_team","", IndexTypeEnum.IDX_SAAS_REFUND_TEAM),
    ORDER_TEAM(XbbRefTypeEnum.ORDER.getCode(),"enum.userTeam.attr.contract_team","", IndexTypeEnum.IDX_SAAS_CONTRACT_TEAM),
    DISTRIBUTOR_CONTACT_TEAM(XbbRefTypeEnum.DISTRIBUTOR_CONTACT.getCode(),"enum.userTeam.attr.contact_team","", IndexTypeEnum.IDX_SAAS_CONTACT_TEAM),
    /**
     * PAAS团队，现在业务上PAAS表单基本都传0，为了减少改动影响，只能这边维护进去，不根据SAAS标识去区分
     */
    PAAS_TEAM(BasicConstant.ZERO, "enum.userTeam.attr.paas_team", "", IndexTypeEnum.IDX_FORM_DATA_TEAM),

    /**
     * 访客计划团队，用于关联新建访客计划
     */
    COMMUNICATE_PLAN_TEAM(XbbRefTypeEnum.COMMUNICATE_PLAN.getCode(),"enum.userTeam.attr.communicate_plan_team","",IndexTypeEnum.IDX_SAAS_COMMUNICATE_PLAN_TEAM),
    /**
     * 新工单
     */
    WORK_ORDER_V2_TEAM(XbbRefTypeEnum.WORK_ORDER_V2.getCode(),"enum.userTeam.attr.work_order_v2_team","",IndexTypeEnum.IDX_WORK_ORDER_V2_TEAM),
    /**
     * 服务项目
     */
    SERVICE_PROJECT_TEAM(XbbRefTypeEnum.SERVICE_PROJECT.getCode(),"enum.userTeam.attr.service_project_team","",IndexTypeEnum.IDX_SERVICE_PROJECT_TEAM),
    /**
     * 回执单
     */
    RECEIPT_ORDER_TEAM(XbbRefTypeEnum.RECEIPT_ORDER.getCode(),"enum.userTeam.attr.receipt_order_team","",IndexTypeEnum.IDX_RECEIPT_ORDER_TEAM),
    /**
     * sla日志团队
     */
    SLA_LOG_TEAM(XbbRefTypeEnum.SLA_LOG.getCode(),"SLA日志团队","",IndexTypeEnum.IDX_SLA_LOG_DATA_TEAM),

    /**
     * 竞争对手
     */
    COMPETITOR_TEAM(XbbRefTypeEnum.COMPETITOR.getCode(),"","",IndexTypeEnum.IDX_SAAS_COMPETITOR_TEAM),


    UNKNOW(XbbRefTypeEnum.UNKNOWN.getCode(),"enum.userTeam.attr.unknow","", null)
    ;
    /**
     * 业务类型
     */
    private Integer code;
    /**
     * 协同团队名称
     */
    private String attr;
    /**
     * 协同团队别称
     */
    private String alias;

    private IndexTypeEnum indexTypeEnum;

    UserTeamEnum(Integer code, String attr, String alias, IndexTypeEnum indexTypeEnum) {
        this.code = code;
        this.attr = attr;
        this.alias = alias;
        this.indexTypeEnum = indexTypeEnum;
    }

    /**
     *
     * @param code
     * @return
     */
    public static UserTeamEnum getByCode(Integer code){
        //付款单和回款单类型转换
        List<Integer> paymentSheetTypeList = PaymentSheetTypeEnum.getAllBusinessType();
        List<Integer> paySheetTypeList = PaySheetTypeEnum.getAllBusinessType();
        if(paymentSheetTypeList.contains(code)){
            code = XbbRefTypeEnum.PAYMENT_SHEET.getCode();
        }else if(paySheetTypeList.contains(code)){
            code = XbbRefTypeEnum.PAY_SHEET.getCode();
        }
        for (UserTeamEnum userTeamEnum : values()){
            if (Objects.equals(userTeamEnum.code,code)){
                return userTeamEnum;
            }
        }
        return UNKNOW;
    }

    public static Boolean hasTeam(Integer businessType) {
        //付款单和回款单类型转换
        List<Integer> paymentSheetTypeList = PaymentSheetTypeEnum.getAllBusinessType();
        List<Integer> paySheetTypeList = PaySheetTypeEnum.getAllBusinessType();
        if(paymentSheetTypeList.contains(businessType)){
            businessType = XbbRefTypeEnum.PAYMENT_SHEET.getCode();
        }else if(paySheetTypeList.contains(businessType)){
            businessType = XbbRefTypeEnum.PAY_SHEET.getCode();
        }
        for (UserTeamEnum userTeamEnum : values()){
            if (Objects.equals(userTeamEnum.getCode(),businessType) && !Objects.equals(userTeamEnum.getCode(),XbbRefTypeEnum.UNKNOWN.getCode())){
                return true;
            }
        }
        return false;
    }

    public static boolean esTeamData(Integer businessType) {
        for (UserTeamEnum userTeamEnum : values()){
            if (Objects.equals(userTeamEnum.getCode(),businessType) && !Objects.equals(userTeamEnum.getCode(),WAREHOUSE_TEAM.getCode()) && !Objects.equals(userTeamEnum.getCode(),UNKNOW.getCode())){
                return true;
            }
        }
        return false;
    }

    public String getAttr() {
        return I18nMessageUtil.getMessage(attr);
    }
}
